//
//  TGTabBar.swift
//  TGWeiBo
//
//  Created by 涛哥无敌威武 on 16/4/17.
//  Copyright © 2016年 taoge. All rights reserved.
//

import UIKit
//声明一个协议
protocol TGTabBarDeletate:NSObjectProtocol{

    //协议方法
    func composeButtonClick()

}
class TGTabBar: UITabBar {
    
    weak var TGDeletate:TGTabBarDeletate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    //如果重写init方法,必须实现init coder方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //监听协议方法
   @objc private func composeButtonClick(){

    TGDeletate?.composeButtonClick()
    
    }

    
    
    //MARK: 懒加载加号按钮
    private lazy var composeButton:UIButton = {
        
        //创建按钮
        let button = UIButton()
        //添加按钮点击事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置image
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        return button
    }()
    

    
    // MARK: 对子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置宽度
        let width = SCREENW / 5
        
        //标识
        var index:CGFloat = 0
        
        //遍历tabbar上的子控件
        for value in self.subviews{
        
            //先判断是否是我们需要的UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                value.frame.origin.x = width * index
                value.frame.size.width = width
                index++
                if index == 2{
                
                index++
                
                }
            }
        }
        //设置composeButton的中心点的x和y
//        composeButton.center = self.center
        composeButton.center.x = self.frame.width / 2
        composeButton.center.y = self.frame.height / 2

        
    }
    
    // MARK:添加加号按钮
    func setUpUI(){
    addSubview(composeButton)
    
    }
    
   }
